import {isEmpty} from '@/utils/CommonUtils';
import {DefaultSystemFields} from '@/components/form/DefaultFormDefinition';

export default class {
  constructor(formDefinition) {
    this.formDefinition = formDefinition;
  }

  getFieldVos() {
    let {fields, subForms} = this.formDefinition, res = [];
    res.push(...this.getMainFieldVos(fields));
    res.push(...this.getSubFormFieldVos(subForms));
    return res;
  }

  getMainFieldVos(fields) {
    let res = [];
    fields.forEach(field => {
      let temp = {
        formShowName: this.formDefinition.showName,
        formName: this.formDefinition.formName,
        formField: field.tableFieldName,
        formFieldVariable: field.tableFieldName,
        fieldComponent: field.component,
        formFieldVariableShowName: undefined,
        main: true,
        fieldShowName: field.showName,
        ...field
      };
      temp.formFieldVariableShowName = `[主表]${field.showName}`;
      res.push(temp);
    });
    return res;
  }

  getAllFieldsLeftForRelationMenuButton() {
    let res = [];
    let fields = this.formDefinition.fields;
    if (isEmpty(fields)) fields = [];
    fields.forEach(f => {
      res.push({
        id: f.id,
        fieldName: f.tableFieldName,
        mainField: true,
        parentFormName: this.formDefinition.formName,
        formName: this.formDefinition.formName,
        formId: this.formDefinition.id,
        component: f.component,
        showName: `[主表]${f.showName}`,
      })
    })
    let subForms = this.formDefinition.subForms
    if(isEmpty(subForms)) subForms = []
    subForms.forEach(subForm => {
      fields = subForm.fields;
      if(isEmpty(fields)) fields = []

      fields.forEach(f => {
        res.push({
          id: f.id,
          fieldName: f.tableFieldName,
          mainField: false,
          formName: subForm.formName,
          formId: subForm.id,
          parentFormName: this.formDefinition.formName,
          component: f.component,
          showName: `[${subForm.showName}]${f.showName}`,
        })
      })
    })
    return res
  }

  getSubFormFieldVos(subForms) {
    if (isEmpty(subForms)) return [];
    let res = [];
    subForms.forEach(subForm => {
      let {fields, formName, showName} = subForm;
      if (isEmpty(fields)) fields = [];
      fields.forEach(field => {
        let temp = {
          formShowName: showName,
          formName: formName,
          formField: field.tableFieldName,
          formFieldVariable: `${formName}.${field.tableFieldName}`,
          formFieldVariableShowName: `[${showName}]${field.showName}`,
          main: false,
          fieldComponent: field.component,
          fieldShowName: field.showName,
          ...field
        };
        res.push(temp);
      });

      // res.push({
      //   formShowName: showName,
      //   formName: formName,
      //   formField: 'create_time',
      //   formFieldVariable: `${formName}.create_time`,
      //   formFieldVariableShowName: `[${showName}]创建时间`,
      //   main: false,
      //   fieldComponent: `TextInput`,
      //   fieldShowName: '创建时间'
      // });
    });
    return res;
  }

  getMainFields() {
    return [...DefaultSystemFields, ...this.formDefinition.fields];
  }

  parseFieldNamesByNames(fieldNames) {
    if (isEmpty(fieldNames)) return;
    let fields = this.findFieldsByNames(fieldNames);
    if (isEmpty(fields)) return;
    fieldNames = fields.map(f => f.showName);
    return fieldNames.toString();
  }

  findFieldsByNames(fieldNames) {
    if (isEmpty(fieldNames)) return [];
    fieldNames = fieldNames.split(',');
    let fields = [...this.formDefinition.fields, ...DefaultSystemFields];
    fields = fields.filter(f => fieldNames.findIndex(t => t === f.tableFieldName) !== -1);
    return fields;
  }

  findFieldsByName(fieldName) {
    if (isEmpty(fieldName)) return undefined;
    let field = [...this.formDefinition.fields, ...DefaultSystemFields];
    field = field.find(f => f.tableFieldName === fieldName);
    return field;
  }
}
